package com.beta.resource.model;

import java.io.File;
import java.io.IOException;

import com.beta.resource.utils.BaseFile;
import com.beta.resource.utils.BaseString;
import com.beta.resource.utils.Time;
import com.beta.resource.utils.onStatusChange;

public class UserFile{
    private static final String UNSUPPORTED_ENCODING = "UnsupportedEncodingException";
    private static final int DESC_MAX_LENGTH = 200;
    private static final int TITLE_MAX_LENGTH = 100;
    private static final int CATEGORY_MAX_LENGTH = 20;
    private int id = 0;
    private int uid = 0;
    private int cid = 0;
    private String title;
    private String desc;
    private String path;
    private String category;
    private String timestamp;
    private String date;
    private String time;

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        if (BaseString.isBase64(category)) {
            this.category = category;
        } else {
            this.category = BaseString.encodeBase64(
                    BaseString.subString(category, CATEGORY_MAX_LENGTH),
                    UNSUPPORTED_ENCODING);
        }
    }

    public String getDecodeCategory() {
        return BaseString.decodeBase64(category, UNSUPPORTED_ENCODING);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public String getTitle() {
        return title;
    }

    public String getDecodeTitle() {
        return BaseString.decodeBase64(title, UNSUPPORTED_ENCODING);
    }

    public void setTitle(String title) {
        if (BaseString.isBase64(title)) {
            this.title = title;
        } else {
            this.title = BaseString.encodeBase64(
                    BaseString.subString(title, TITLE_MAX_LENGTH),
                    UNSUPPORTED_ENCODING);
        }
    }

    public String getDesc() {
        return desc;
    }

    public String getDecodeDesc() {
        return BaseString.decodeBase64(desc, UNSUPPORTED_ENCODING);
    }

    public void setDesc(String desc) {
        if (BaseString.isBase64(desc)) {
            this.desc = desc;
        } else {
            this.desc = BaseString.encodeBase64(
                    BaseString.subString(desc, DESC_MAX_LENGTH),
                    UNSUPPORTED_ENCODING);
        }
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        if (BaseString.isBase64(path)) {
            this.path = path;
        } else {
            this.path = BaseString.encodeBase64(path, UNSUPPORTED_ENCODING);
        }
    }

    public String getDecodePath() {
        return BaseString.decodeBase64(path, UNSUPPORTED_ENCODING);
    }

    public String getDate() {
        return date;
    }

    public String getTime() {
        return time;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
        String[] emt = timestamp.split(" ");
        date = emt[0];
        time = emt[1];
    }

    private String getCurrentTimestamp() {
        return Time.getUTCTime("yyyy/MM/dd HH:mm:ss");
    }

    public boolean isNewOne() {
        return id == 0;
    }

    public void preSave(String root, int uid, int cid, boolean isnew, String notes) {
        setUid(uid);
        setCid(cid);
        String path = root + File.separator + getUid() + File.separator + getCid();
        path = BaseFile.createNewFile(path, getDecodeTitle(), false);
        BaseFile.appendFile(path, notes.getBytes(), 0);
        setTimestamp(getCurrentTimestamp());
        setPath(path);
    }

    public int append(String path, long offset, String msg) {
        return BaseFile.appendFile(path, msg.getBytes(), offset);
    }

    public void read(onStatusChange listener, Object data) throws IOException{
        BaseFile.loadFile(getDecodePath(), listener, data);
    }
}
